#include "Triangle.h"

Triangle::Triangle()
{
	BuildVertexBuffer();
}

Triangle::~Triangle()
{
	ReleaseCOM(mVB);
}

void Triangle::Update(double dt)
{

}

void Triangle::Draw(ID3DXSprite& sprite, D3DXMATRIX& view, D3DXMATRIX& proj)
{
	HR(gD3DDevice->SetTransform(D3DTS_VIEW, &view));
	HR(gD3DDevice->SetTransform(D3DTS_PROJECTION, &proj));

	HR(gD3DDevice->SetStreamSource(0, mVB, 0, sizeof(VertexPosCol)));
	HR(gD3DDevice->SetVertexDeclaration(VertexPosCol::decl));
	HR(gD3DDevice->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 3));
}

void Triangle::BuildVertexBuffer()
{
	HR(gD3DDevice->CreateVertexBuffer(
		3 * sizeof(VertexPosCol),
		D3DUSAGE_WRITEONLY,
		0, D3DPOOL_MANAGED,
		&mVB, 0));

	VertexPosCol* v = 0;
	mVB->Lock(0, 0, (void**)&v, 0);

	v[0] = VertexPosCol(-1.0f, 0.0f, 0.0f);
	v[1] = VertexPosCol(0.0f, 1.0f, 0.0f);
	v[2] = VertexPosCol(1.0f, 0.0f, 0.0f);

	mVB->Unlock();
}
